1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import fs from 'fs'
|
7 | import path from 'path'
|
8 | import Module from 'module'
|
9 |
|
10 | import Log from './tools/log'
|
11 | import { exists, starts_with, ends_with } from './helpers'
|
12 |
|
13 | import serialize from './tools/serialize-javascript'
|
14 |
|
15 | const require_hacker =
|
16 | {
|
17 | preceding_path_resolvers: [],
|
18 | path_resolvers: [],
|
19 |
|
20 | global_hook_resolved_modules: {},
|
21 |
|
22 | global_hooks_enabled: true,
|
23 |
|
24 | occupied_file_extensions: new Set(),
|
25 |
|
26 |
|
27 | log: new Log('require-hook', { debug: false }),
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | global_hook(id, resolver, options = {})
|
61 | {
|
62 | validate.global_hook(id, resolver)
|
63 |
|
64 | const resolver_entry =
|
65 | {
|
66 | id,
|
67 | resolve: (path, module) =>
|
68 | {
|
69 | const resolved_path = `${path}.${id}`
|
70 |
|
71 |
|
72 | const source = resolver(path, module)
|
73 |
|
74 | if (!exists(source))
|
75 | {
|
76 | return
|
77 | }
|
78 |
|
79 |
|
80 | delete require.cache[resolved_path]
|
81 |
|
82 | require_hacker.global_hook_resolved_modules[resolved_path] = source
|
83 |
|
84 | return resolved_path
|
85 | }
|
86 | }
|
87 |
|
88 | if (options.precede_node_loader === false)
|
89 | {
|
90 | require_hacker.path_resolvers.push(resolver_entry)
|
91 | }
|
92 | else
|
93 | {
|
94 | require_hacker.preceding_path_resolvers.push(resolver_entry)
|
95 | }
|
96 |
|
97 | const hook = this.hook(id, path =>
|
98 | {
|
99 | const source = require_hacker.global_hook_resolved_modules[path]
|
100 | delete require_hacker.global_hook_resolved_modules[path]
|
101 | return source
|
102 | })
|
103 |
|
104 | const result =
|
105 | {
|
106 | unmount: () =>
|
107 | {
|
108 |
|
109 | require_hacker.preceding_path_resolvers = require_hacker.preceding_path_resolvers.filter(x => x !== resolver_entry)
|
110 | require_hacker.path_resolvers = require_hacker.path_resolvers.filter(x => x !== resolver_entry)
|
111 | hook.unmount()
|
112 | }
|
113 | }
|
114 |
|
115 | return result
|
116 | },
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | hook(extension, resolve)
|
133 | {
|
134 | this.log.debug(`Hooking into *.${extension} files loading`)
|
135 |
|
136 |
|
137 | validate.extension(extension)
|
138 | validate.resolve(resolve)
|
139 |
|
140 |
|
141 | this.occupied_file_extensions.add(extension)
|
142 |
|
143 |
|
144 | const dot_extension = `.${extension}`
|
145 |
|
146 |
|
147 | const original_loader = Module._extensions[dot_extension]
|
148 |
|
149 |
|
150 | if (original_loader)
|
151 | {
|
152 |
|
153 |
|
154 | this.log.debug(`-----------------------------------------------`)
|
155 | this.log.debug(`Overriding an already existing require() hook `)
|
156 | this.log.debug(`for file extension ${dot_extension}`)
|
157 | this.log.debug(`-----------------------------------------------`)
|
158 | }
|
159 |
|
160 |
|
161 | const cached_modules = new Set()
|
162 |
|
163 |
|
164 |
|
165 | if (!Module._extensions)
|
166 | {
|
167 | throw new Error('Incompatilbe Node.js version detected: "Module._extensions" array is missing. File an issue on GitHub.')
|
168 | }
|
169 |
|
170 |
|
171 | Module._extensions[dot_extension] = (module, filename) =>
|
172 | {
|
173 | this.log.debug(`Loading source code for ${filename}`)
|
174 |
|
175 |
|
176 | const source = resolve(filename, module)
|
177 |
|
178 | if (!exists(source))
|
179 | {
|
180 | this.log.debug(`Fallback to original loader`)
|
181 |
|
182 |
|
183 |
|
184 | if (path.extname(filename) !== dot_extension)
|
185 | {
|
186 | this.log.info(`Trying to load "${path.basename(filename)}" as a "*${dot_extension}"`)
|
187 | }
|
188 |
|
189 |
|
190 | return (original_loader || Module._extensions['.js'])(module, filename)
|
191 | }
|
192 |
|
193 |
|
194 | cached_modules.add(filename)
|
195 |
|
196 |
|
197 |
|
198 | if (!module._compile)
|
199 | {
|
200 | throw new Error('Incompatilbe Node.js version detected: "Module.prototype._compile" function is missing. File an issue on GitHub.')
|
201 | }
|
202 |
|
203 |
|
204 |
|
205 | module._compile(source, filename)
|
206 | }
|
207 |
|
208 | const result =
|
209 | {
|
210 |
|
211 | unmount: () =>
|
212 | {
|
213 |
|
214 | for (let path of cached_modules)
|
215 | {
|
216 | delete require.cache[path]
|
217 | }
|
218 |
|
219 |
|
220 | Module._extensions[dot_extension] = original_loader
|
221 |
|
222 |
|
223 | this.occupied_file_extensions.delete(extension)
|
224 | }
|
225 | }
|
226 |
|
227 | return result
|
228 | },
|
229 |
|
230 |
|
231 | to_javascript_module_source(anything)
|
232 | {
|
233 |
|
234 | if (!exists(anything))
|
235 | {
|
236 | return 'module.exports = undefined'
|
237 | }
|
238 |
|
239 |
|
240 | if (typeof anything === 'string' && is_a_module_declaration(anything))
|
241 | {
|
242 | return anything
|
243 | }
|
244 |
|
245 |
|
246 | return 'module.exports = ' + serialize(anything)
|
247 | },
|
248 |
|
249 |
|
250 |
|
251 | resolve(path_to_resolve, module)
|
252 | {
|
253 |
|
254 |
|
255 |
|
256 | if (starts_with(path_to_resolve, '/'))
|
257 | {
|
258 | return path_to_resolve
|
259 | }
|
260 |
|
261 |
|
262 | if (path_to_resolve === '.'
|
263 | || path_to_resolve === '..'
|
264 | || starts_with(path_to_resolve, './')
|
265 | || starts_with(path_to_resolve, '../'))
|
266 | {
|
267 | return path.resolve(path.dirname(module.filename), path_to_resolve)
|
268 | }
|
269 |
|
270 |
|
271 | try
|
272 | {
|
273 | require_hacker.global_hooks_enabled = false
|
274 | return Module._resolveFilename(path_to_resolve, module)
|
275 | }
|
276 | finally
|
277 | {
|
278 | require_hacker.global_hooks_enabled = true
|
279 | }
|
280 | }
|
281 | }
|
282 |
|
283 |
|
284 | const validate =
|
285 | {
|
286 | extension(extension)
|
287 | {
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | if (path.extname(`test.${extension}`) !== `.${extension}`)
|
294 | {
|
295 | throw new Error(`Invalid file extension "${extension}"`)
|
296 | }
|
297 |
|
298 |
|
299 | if (require_hacker.occupied_file_extensions.has(extension))
|
300 | {
|
301 | throw new Error(`File extension "${extension}" is already occupied by require-hacker`)
|
302 | }
|
303 | },
|
304 |
|
305 | resolve(resolve)
|
306 | {
|
307 | if (typeof resolve !== 'function')
|
308 | {
|
309 | throw new Error(`Resolve should be a function. Got "${resolve}"`)
|
310 | }
|
311 | },
|
312 |
|
313 | global_hook(id, resolver)
|
314 | {
|
315 | if (!id)
|
316 | {
|
317 | throw new Error(`You must specify global hook id`)
|
318 | }
|
319 |
|
320 | if (path.extname(`test.${id}`) !== `.${id}`)
|
321 | {
|
322 | throw new Error(`Invalid global hook id "${id}". Expected a valid file extension.`)
|
323 | }
|
324 |
|
325 |
|
326 | if (require_hacker.occupied_file_extensions.has(id))
|
327 | {
|
328 | throw new Error(`File extension "${id}" is already occupied by require-hacker`)
|
329 | }
|
330 |
|
331 | validate.resolve(resolver)
|
332 | }
|
333 | }
|
334 |
|
335 |
|
336 |
|
337 | if (!Module._resolveFilename)
|
338 | {
|
339 | throw new Error('Incompatilbe Node.js version detected: "Module._resolveFilename" function is missing. File an issue on GitHub.')
|
340 | }
|
341 |
|
342 |
|
343 |
|
344 | if (!Module._findPath)
|
345 | {
|
346 | throw new Error('Incompatilbe Node.js version detected: "Module._findPath" function is missing. File an issue on GitHub.')
|
347 | }
|
348 |
|
349 |
|
350 | let require_caller
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 | const original_resolveFilename = Module._resolveFilename
|
359 | Module._resolveFilename = function(...parameters)
|
360 | {
|
361 | const request = parameters[0]
|
362 | const parent = parameters[1]
|
363 |
|
364 |
|
365 | require_caller = parent
|
366 |
|
367 | return original_resolveFilename.apply(this, parameters)
|
368 | }
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | const original_findPath = Module._findPath
|
376 | Module._findPath = (...parameters) =>
|
377 | {
|
378 | const request = parameters[0]
|
379 |
|
380 |
|
381 |
|
382 | if (require_hacker.global_hooks_enabled)
|
383 | {
|
384 | for (let resolver of require_hacker.preceding_path_resolvers)
|
385 | {
|
386 | const resolved = resolver.resolve(request, require_caller)
|
387 | if (exists(resolved))
|
388 | {
|
389 | return resolved
|
390 | }
|
391 | }
|
392 | }
|
393 |
|
394 |
|
395 | const filename = original_findPath.apply(undefined, parameters)
|
396 | if (filename !== false)
|
397 | {
|
398 | return filename
|
399 | }
|
400 |
|
401 |
|
402 | if (require_hacker.global_hooks_enabled)
|
403 | {
|
404 | for (let resolver of require_hacker.path_resolvers)
|
405 | {
|
406 | const resolved = resolver.resolve(request, require_caller)
|
407 | if (exists(resolved))
|
408 | {
|
409 | return resolved
|
410 | }
|
411 | }
|
412 | }
|
413 |
|
414 | return false
|
415 | }
|
416 |
|
417 |
|
418 | function is_a_module_declaration(text)
|
419 | {
|
420 | return text.indexOf('module.exports = ') === 0 ||
|
421 | /\s+module\.exports = .+/.test(text)
|
422 | }
|
423 |
|
424 | export default require_hacker |
\ | No newline at end of file |